/**
 * 
 */
package filters;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import metier.Utilisateur;

import org.apache.log4j.Logger;

/**
 * @author hozakan
 *
 */
public class LoginFilter implements Filter {
	
	private static final Logger LOGGER = Logger.getLogger(LoginFilter.class);

	/* (non-Javadoc)
	 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
	 */
	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see javax.servlet.Filter#destroy()
	 */
	@Override
	public void destroy() {}

	/* (non-Javadoc)
	 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
	 */
	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain filterChain) throws IOException, ServletException {
		
		HttpServletRequest  httpServletRequest  = (HttpServletRequest)  request;
	    HttpServletResponse httpServletResponse = (HttpServletResponse) response;
	    HttpSession httpSession = httpServletRequest.getSession(false);
	    
	    String mapping = "/pages/login.jsp";
	    Utilisateur user = (Utilisateur) httpSession.getAttribute("user");
	    if (user != null) {
		    if (LOGGER.isDebugEnabled()) {
		    	LOGGER.debug("détails de l'utilisateur passé au filtre :");
		    	LOGGER.debug("nom : "+user.getNom());
		    	LOGGER.debug("prenom : "+user.getPrenom());
		    	LOGGER.debug("login : "+user.getLogin());
		    	LOGGER.debug("rang : "+user.getRang());
	    	}
	    }
	    if (user == null) {
	    	if (LOGGER.isDebugEnabled()) {
	    		LOGGER.debug("l'utilisateur est null"); 
	    	}
	    	String uri = httpServletRequest.getRequestURI();
	    	uri = uri.substring(uri.indexOf("x")+1, uri.length());
	    	httpSession.setAttribute("redirect", uri);
	    	httpServletRequest.getRequestDispatcher(mapping).forward(httpServletRequest, httpServletResponse);
	    } else if (!"administrateur".equals(user.getRang())) {
	    	if (LOGGER.isDebugEnabled()) {
	    		LOGGER.debug("l'utilisateur n'est pas administrateur");
	    	}
	    	List<String> errors = new ArrayList<String>();
	    	errors.add("Connectez-vous en tant qu'administrateur pour accéder à cette page");
	    	httpSession.setAttribute("errors", errors);
	    	httpServletRequest.getRequestDispatcher("/pages/error.jsp").forward(httpServletRequest, httpServletResponse);
	    } else {
	    	if (LOGGER.isDebugEnabled()) {
	    		LOGGER.debug("l'utilisateur est administrateur!!");
	    	}
	    	filterChain.doFilter(request, response);
	    }
		    
	}

}
